﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ELMApplication.Web.DataModel;
using ELMApplication.Web;
using System.Xml.Linq;
using System.Data.Objects;


    public static class Util
    {
        public static Human ConvertToHuman(this t_Human tHuman,ELMEntities db)
        {
            
            Human hm = null;
            if (tHuman != null)
             hm = new Human()
            {
                DutyId = tHuman.t_Duty.DutyId,
                HumanDuty = tHuman.t_Duty.DutyName,
                HumanId = tHuman.Human_ID,
                HumanName = tHuman.Human_Name,
                Password=tHuman.Password,
                Attributes=tHuman.Attribute.ConvertXmlToFieldsOnlyValue(db),
            };
            return hm;
        }

        public static Matter ConvertToMatter(this t_Matter tmatter,ELMEntities db)
        {

            Matter mt = null;
            if (tmatter != null)
            {
                mt = new Matter()
                {
                    MatterId = tmatter.Matter_ID,
                    MatterName = tmatter.Matter_Name,
                    Attributes = tmatter.Attribute.ConvertXmlToFieldsOnlyValue(db)
                };
            }
            return mt;
        }
        public static ELMField ConvertToFieldType(this t_Field tField)
        {

            ELMField mt = null;
            if (tField != null)
            {
                mt = new ELMField()
                {
                    FieldId=tField.FieldId,
                    FieldName=tField.FieldName,
                    FieldType=tField.t_FieldType.FieldTypeId,
                    FieldGroupId=tField.t_FieldGroup.FieldGroupId,
                    FieldGroupName=tField.t_FieldGroup.GroupName,
                    
                    
                };
            }
            return mt;
        }
        public static t_ELMList ConverTot_ELMList(this ELMList data,ELMEntities db)
        {
            t_ELMList mt = null;
            if (data != null)
            {
                mt = new t_ELMList()
                {
                    ID=data.ELMListId,
                    ELMListName=data.ELMListName,
                    t_ContentType=db.t_ContentType.First(c=>c.ContentTypeId==data.ContentType.ContentId),
                };

            }
            return mt;
        }
        public static t_ELMItems ConverTot_ELMItem(this ELMItem data, ELMEntities db)
        {
            t_ELMItems mt = null;
            if (data != null)
            {
                mt = new t_ELMItems()
                {
                   DataAttribute=data.Datas.ConvertToXMLDocumentOnlyValue(),
                   ELMItemId=data.ELMItemId,
                   t_ELMList=db.t_ELMList.First(c=>c.ID==data.ELMList.ELMListId),                   
                };

            }
            return mt;
        }
        public static ContentType ConvertToContentType(this t_ContentType tCT, ELMEntities db)
        {

            ContentType mt = null;
            if (tCT != null)
            {
                mt = new ContentType()
                {
                    ContentId = tCT.ContentTypeId,
                    ContentName = tCT.ContentTypeName,
                    Fields = tCT.ContentFields.ConvertXmlToFieldsWithTemplateAttribute(db),


                };
            }
            return mt;
        }
        public static ELMList ConvertToELMList(this t_ELMList tLIST,ELMEntities db)
        {

            ELMList mt = null;
            if (tLIST != null)
            {
                mt = new ELMList()
                {
                    ELMListId = tLIST.ID,
                    ContentType = tLIST.t_ContentType.ConvertToContentType(db),
                    ELMListName=tLIST.ELMListName,
                };
            }
            return mt;
        }
        public static ELMItem ConvertToELMItem(this t_ELMItems tItem,ELMEntities db)
        {

            ELMItem mt = null;
            if (tItem != null)
            {
                mt = new ELMItem()
                {
                   ELMItemId=tItem.ELMItemId,
                   ELMList=tItem.t_ELMList.ConvertToELMList(db),
                   Datas=tItem.DataAttribute.ConvertXmlToFieldsOnlyValue(db)
                };
            }
            return mt;
        }

        public static List<ELMField> ConvertToFieldTypeList(this IQueryable<t_Field> fieldList)
        {
            var list=fieldList.ToList();
            List<ELMField> ds = new List<ELMField>();
            foreach (var item in list)
            {
                ds.Add(item.ConvertToFieldType());
            }
            return ds;

        }

        public static ELMApplication.Web.DataModel.Attachment ConvertToAttachment(this ELMApplication.Web.Attachment att)
        {

            ELMApplication.Web.DataModel.Attachment mt = null;
            if (att != null)
            {
                mt = new ELMApplication.Web.DataModel.Attachment()
                {
                    Guid = att.MailGuid.ToString(),
                    Name = att.FileName,
                };
            }
            return mt;
        }
        public static string ConvertToXMLDocumentWithTemplateAttribute(this Dictionary<ELMField, string> data)
        {
            string str = "<root>";
            foreach (var item in data)
            {
                string itemsourceguid="";
                string displaymemberGuid = "";
                if(item.Key.ItemSourceGuid.HasValue)
                {
                    itemsourceguid = " ItemSourceGuid='" + item.Key.ItemSourceGuid.Value.ToString() + "' ";
                }
                if (item.Key.DisplayMemberGuid.HasValue)
                {
                    displaymemberGuid = " DisplayMemberGuid='" + item.Key.DisplayMemberGuid.Value.ToString() + "' ";
                }
                str += "<Field Id='" + item.Key.FieldId.ToString() +"' "+ displaymemberGuid + itemsourceguid + " IsDisplayInELMList='" + item.Key.IsDisplayInELMList.ToString() + "'>" + item.Value + "</Field>";
            }
            str+="</root>";
            return str;
        }
        public static string ConvertToXMLDocumentOnlyValue(this Dictionary<ELMField, string> data)
        {
            string str = "<root>";
            foreach (var item in data)
            {
                str += "<Field Id='" + item.Key.FieldId.ToString() + "'>" + item.Value + "</Field>";
            }
            str += "</root>";
            return str;
        }

        public static Dictionary<ELMField, string> ConvertXmlToFieldsOnlyValue(this string data,ELMEntities db)
        {
            Dictionary<ELMField, string> ds = new Dictionary<ELMField, string>();
            try
            {
                XElement root = XElement.Parse(data);
                foreach (var item in root.Descendants())
                {
                    var att = item.Attribute(XName.Get("Id"));
                    Guid value = new Guid(att.Value);
                    var fielddata = db.t_Field.Include("t_FieldGroup").Include("t_FieldType").First(c => c.FieldId == value).ConvertToFieldType();
                  
                    ds.Add(fielddata, item.Value);
                }
                
            }
            catch (Exception ex) { throw ex; }
            return ds;
        }

        public static Dictionary<ELMField, string> ConvertXmlToFieldsWithTemplateAttribute(this string data, ELMEntities db)
        {
            Dictionary<ELMField, string> ds = new Dictionary<ELMField, string>();
            try
            {
                XElement root = XElement.Parse(data);
                foreach (var item in root.Descendants())
                {
                    var att = item.Attribute(XName.Get("Id"));
                    Guid value = new Guid(att.Value);
                    var fielddata = db.t_Field.Include("t_FieldGroup").Include("t_FieldType").First(c => c.FieldId == value).ConvertToFieldType();

                    var IsDisplayInELMList = item.Attribute(XName.Get("IsDisplayInELMList"));
                    var displaymemberguid = item.Attribute(XName.Get("DisplayMemberGuid"));
                    var itemsourceguid = item.Attribute(XName.Get("ItemSourceGuid"));
                    if (IsDisplayInELMList != null)
                    {
                        fielddata.IsDisplayInELMList = bool.Parse(IsDisplayInELMList.Value);
                    }
                    if (displaymemberguid != null)
                    {
                        fielddata.DisplayMemberGuid = new Guid(displaymemberguid.Value);
                    }
                    if (itemsourceguid != null)
                    {
                        fielddata.ItemSourceGuid = new Guid(itemsourceguid.Value);
                    }
                    ds.Add(fielddata, item.Value);
                }

            }
            catch (Exception ex) { throw ex; }
            return ds;
        }
        
    }

